package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询用户购物车
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //从当前线程中获取用户id
        Long userId = BaseContext.getCurrentId();
        //select * from shopping_cart where user_id=?
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return R.success(list);
    }

    /**
     * 添加购物项
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //1.判断用户的购物车中是否存在该购物项
        //select count(1) from shopping_cart where user_id=? and dish_id=?
        //select count(1) from shopping_cart where user_id=? and setmeal_id=?
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if (one != null) {
            //说明购物车有数据,需要修改操作
            //在one的基础上,将数量+1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            return R.success(one);
        } else {
            //说明购物车没有数据,需要添加操作
            //设置用户id
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean(){
        //根据用户id删除
        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .remove();
        return R.success("清空成功");
    }

    /**
     * 移除购物项
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //1.判断用户的购物车中是否存在该购物项
        //select count(1) from shopping_cart where user_id=? and dish_id=?
        //select count(1) from shopping_cart where user_id=? and setmeal_id=?
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if(one.getNumber()>1){
            //说明需要修改操作,在one的基础上,将数量-1
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        } else {
            //说明需要删除操作
            shoppingCartService.removeById(one.getId());
            //TODO:主要是解决前端数量显示问题
            one.setNumber(0);
        }
        return R.success(one);
    }

}
